17 memset(number
,0,sizeof(number
));
26 // takes 'r' as right number and writes result to 'this'
28 HugeInt
* operator= (const HugeInt
* r
) {
29 memset(number
,0,sizeof(number
));
30 strcpy(number
,r
->number
);
36 HugeInt
* operator= (const int r
) {
37 memset(number
,0,sizeof(number
));
38 sprintf(number
,"%d",r
);
39 length
= strlen(number
);
40 for (int i
= 0; i
< (length
>> 1); i
++) {
42 number
[i
] = number
[length
-i
-1];
43 number
[length
-i
-1] = c
;
45 for (int j
= 0; j
< length
; j
++)
51 const HugeInt
operator+ (const HugeInt
& r
) {
52 int n
= max(length
,r
.length
), carry
= 0, k
, i
;
54 for (i
= 0; i
< n
|| carry
; i
++) {
55 k
= number
[i
] + r
.number
[i
] + carry
;
56 theNew
.number
[i
] = k
% 10;
64 void operator+= (const HugeInt
& r
) {
68 // *slightly veryfied*
69 const HugeInt
operator* (const int r
) {
72 for (i
= 0; i
< length
|| carry
; i
++) {
73 k
= number
[i
] * r
+ carry
;
74 theNew
.number
[i
] = k
% 10;
81 // *slightly veryfied*
82 void operator*= (const int r
) {
87 // shifts number left by 'shift' positions
88 // useful when multiplying two HugeInt's
89 HugeInt
operator<< (const int shift
) {
92 // don't shift if number is 0 and there is no number
93 //if (length == 0 || length == 1 && number[0] == 0) return;
94 for (i
= length
- 1; i
>= 0; i
--)
95 theNew
.number
[i
+ shift
] = number
[i
];
96 for (i
= 0; i
< shift
; i
++)
98 theNew
.length
= length
+ shift
;
102 HugeInt
operator* (HugeInt
& r
) {
105 for (i
= 0; i
< length
; i
++)
106 theNew
+= (r
<< i
) * number
[i
];
111 HugeInt
operator*= (HugeInt
& r
) {
116 int operator % (int r
) {
118 for (int i
= length
- 1; i
>= 0; i
--)
119 n
= (n
* 10 + number
[i
]) % r
;
123 int operator %= (int r
) {
127 HugeInt
operator/ (int r
) {
131 for (int i
= length
- 1; i
>= 0 || n
>= r
; i
--) {
132 n
= (n
* 10 + number
[i
]);
133 I
.number
[j
++] = n
/ r
;
138 for (int i
= 0; i
< j
/ 2; i
++)
139 swap(I
.number
[i
],I
.number
[j
-i
-1]);
146 HugeInt
operator /= (int r
) {
153 for (int i
= length
- 1; i
>= 0; i
--)
154 putchar(number
[i
] + '0');
157 void truncate(HugeInt
& n
) {
158 for (int i
= n
.length
- 1; i
>= 0 && n
.number
[i
] == 0; i
--)
166 HugeInt
power(int p
) {
168 for (int i
= 0; i
< p
; i
++)
173 bool operator<(const HugeInt
& rhs
) {
174 if (this->length
!= rhs
.length
)
175 return this->length
< rhs
.length
;
176 for (int i
= 0; i
< this->length
; i
++)
177 if (this->number
[i
] != rhs
.number
[i
])
178 return this->number
[i
] < rhs
.number
[i
];
183 if (length
== 1 && number
[0] == 0) {
192 HugeInt dp
[2][10000];
194 dp[i][j] = cantidad de maneras diferentes
195 en que puedo distribuir las primeras i
196 letras de la subsecuencia (b) terminando
197 en la letra j de la secuencia original (a)
209 if (b
.size() > a
.size()){
214 int A
= a
.size(), B
= b
.size();
215 dp
[0][0] = int(a
[0] == b
[0]);
216 for (int j
=1; j
<A
; ++j
){
217 dp
[0][j
] = dp
[0][j
-1] + int(a
[j
] == b
[0]);
220 for (int i
=1; i
<B
; ++i
){
222 for (int j
=1; j
<A
; ++j
){
223 dp
[i
%2][j
] = dp
[i
%2][j
-1];
225 dp
[i
%2][j
] += dp
[(i
+1)%2][j
-1];
229 dp
[(B
-1)%2][A
-1].print();